package com.example.mybatis_book.controller;

import com.example.mybatis_book.constant.Constant;
import com.example.mybatis_book.model.*;
import com.example.mybatis_book.service.BookInfoService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookInfoController {

    @Autowired
    private BookInfoService bookService;

//    @RequestMapping("/getBookList")
//    public List<BookInfo> getBookList(){
//        return bookService.getBookList();
//    }


    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("接收到参数：bookinfo {}", bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookname()) ||
        !StringUtils.hasLength(bookInfo.getAuthor()) ||
        bookInfo.getNum() == null ||
        bookInfo.getPrice() == null ||
        !StringUtils.hasLength(bookInfo.getPublisher()) ||
        bookInfo.getStatus() == null) {
            return "参数不合法";
        }

        try {
            Integer result =  bookService.addBook(bookInfo);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("添加异常, e:",e);
        }
        return "添加失败";
    }

    @RequestMapping("/getBookListByPage")
    public Result<PageResponse<BookInfo>> getBookListByPage(PageRequest  pageRequest, HttpSession  session) {
        log.info("接收到参数：pageRequest {}",pageRequest);
        //session中获取用户信息
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constant.USER_SESSION_KEY);
//        if(userInfo == null || userInfo.getId() <= 0) {
//            return Result.nologin();
//        }

        return Result.success(bookService.getBookListByPage(pageRequest));
    }

    /*查询
    * */
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("接收到参数：id {}",bookId);
        try {
            return bookService. queryBookById(bookId);
        } catch (Exception e) {
            return null;
        }
    }


    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBookById(BookInfo bookInfo) {
        log.info("接收到参数：bookInfo {}",bookInfo);
        try {
            Integer result = bookService.updateBookById(bookInfo);
            log.info("更新结果：{}",result);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("更新异常, e:"+e.getMessage());
        }
        return "更新失败";
    }


    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBookById(Integer bookId) {
        log.info("接收到参数：bookId {}",bookId);
        try {
            Integer result = bookService.deleteBookById(bookId);
            log.info("删除结果：{}",result);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("删除异常, e:"+e.getMessage());
        }
        return "删除失败";
    }

    @RequestMapping(value = "/bathDeleteBook",produces = "application/json")
    public String bathDeleteBook(@RequestParam List<Integer> ids) {
        log.info("批删信息："+ids);
        try {
            Integer result = bookService.bathDeleteBook(ids);
            if (result == ids.size()) {
                return "";
            }
        }catch (Exception e){
            log.error("错误信息："+e.getMessage());
        }
        return "操作失败";
    }
}